﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BizTalk.Message.Interop;
using System.IO;

namespace BizMock
{
    sealed public class ContextProperty
    {
        public ContextProperty(string name, string ns, object value, bool write )
        {
            _name = name;
            _namespace = ns;
            _value = value;
            _promote = write;
        }

        private string _name;
        public string Name
        {
            get { return _name; }
        }

        private string _namespace;
        public string Namespace
        {
            get { return _namespace; }
        }

        private object _value;
        public object Value
        {
            get { return _value; }
        }

        private bool _promote = false;
        public bool Promote
        {
            get { return _promote; }
        }
    }

    sealed public class Message
    {
        private MessageFileHelper.MessagePartInfo[] _msgPartInfo;
        private List<ContextProperty> _promotedProperties = new List<ContextProperty>();

        public Message(params string[] path)
        {
            _msgPartInfo = new MessageFileHelper.MessagePartInfo[path.Length];
            for (int i = 0; i < path.Length; i++)
            {
                _msgPartInfo[i] = new MessageFileHelper.MessagePartInfo
                {
                    Charset = "UTF-8",
                    ContentType = "text/xml",
                    FileName = path[i]
                };
            }
        }

        public Message(params Stream[] str)
        {
            _msgPartInfo = new MessageFileHelper.MessagePartInfo[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                _msgPartInfo[i].Charset = "UTF-8";
                _msgPartInfo[i].ContentType = "text/xml";
                _msgPartInfo[i].Data = str[i];
            }
        }

        public void PromoteProperty(string name, string ns, object value, bool promote)
        {
            _promotedProperties.Add(new ContextProperty(name, ns, value, promote));
        }

        public IBaseMessage CreateRequestMessage(IBaseMessageFactory msgFactory)
        {
            IBaseMessage msg = MessageFileHelper.LoadFromBodyContentsFile(_msgPartInfo[0].FileName, msgFactory);
            for (int i = 1; i < _msgPartInfo.Length; i++)
            {
                IBaseMessagePart part = MessageFileHelper.LoadFromPartContentsFile(_msgPartInfo[i], msgFactory);
                msg.AddPart(string.Format("part_{0}", i), part, false);
            }
            foreach (ContextProperty pp in _promotedProperties)
            {
                if (pp.Promote)
                    msg.Context.Promote(pp.Name, pp.Namespace, pp.Value);
                else
                    msg.Context.Write(pp.Name, pp.Namespace, pp.Value);
            }
            return msg;
        }


    }
}
